package fr.web325.servlet.api.index;

import fr.web325.bean.Json;
import fr.web325.dao.BookDao;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/api/book")
public class Book extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取ID
        String id = req.getParameter("id");
        try {
            fr.web325.bean.Book book = new BookDao().getBookById(id);
            if(book == null){
                resp.getWriter().print(new Json(200,null,"暂无该数据!").getRespJson());
            }else{
                JSONObject bookJson = JSONObject.fromObject(book);
                resp.getWriter().print(new Json(200,bookJson,"获取书籍成功!").getRespJson());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
